package com.huawei.fastengine.internal.fastview;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.fastengine.fastview.FastAppInfo;
import com.huawei.fastengine.fastview.FastViewInstance;
import com.huawei.fastengine.fastview.ICardMessage;
import com.huawei.fastengine.fastview.JSBundleLoader;
import com.huawei.fastengine.fastview.RenderListener;
import com.huawei.fastengine.fastview.RouterEventReceiver;
import com.huawei.fastengine.fastview.WidgetInfo;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import com.huawei.fastengine.fastview.util.FastSDKProfile;
import com.huawei.fastengine.internal.FastSdkLoader;
import com.huawei.fastengine.internal.FastSdkLoader.HostMode;
import com.huawei.fastsdk.IFastCardMessage;
import com.huawei.fastsdk.IFastRenderListener;
import com.huawei.fastsdk.IFastSDKInstance;
import com.huawei.fastsdk.IFastSDKProxy;
import java.util.HashMap;
import java.util.Map;

public class FastViewInstanceImpl extends FastViewInstance implements IFastCardMessage, IFastRenderListener {
    private static final String TAG = "FastSDKEngine";
    private String accessType;
    private ICardMessage iCardMessage;
    private boolean isDestroyed = false;
    private FrameLayout mAttachedRootView;
    private JSBundleLoader mBundleLoader;
    private Context mContext;
    private IFastSDKInstance mInstance;
    private RenderListener mRenderListener;
    private int mWidth;

    static class CardRenderInfo {
        String js;
        String jsonParam;
        String uri;
        WidgetInfo widgetInfo;

        private CardRenderInfo() {
        }
    }

    public FastViewInstanceImpl(Context context, JSBundleLoader jSBundleLoader, RenderListener renderListener, ICardMessage iCardMessage, int i, String str) {
        this.mContext = context;
        this.mBundleLoader = jSBundleLoader;
        this.mRenderListener = renderListener;
        this.iCardMessage = iCardMessage;
        this.mWidth = i;
        this.accessType = str;
    }

    private Runnable getUIRender(final CardRenderInfo cardRenderInfo) {
        return new Runnable() {
            public void run() {
                if (FastViewInstanceImpl.this.isDestroyed) {
                    FastSDKProfile.log("render cancel, destroyed.");
                    return;
                }
                FastSDKProfile.begin("createInstance:" + cardRenderInfo.widgetInfo.getName());
                FastViewInstanceImpl.this.createInstance();
                if (FastViewInstanceImpl.this.mInstance == null) {
                    FastViewLogUtils.e(FastViewInstanceImpl.TAG, "mInstance == null");
                    return;
                }
                FastViewInstanceImpl.this.mInstance.setPackageInfo(FastViewInstanceImpl.this.makePackageInfo(cardRenderInfo.widgetInfo));
                FastViewInstanceImpl.this.mInstance.setTrackComponent(true);
                FastViewInstanceImpl.this.mInstance.setBundleUrl(cardRenderInfo.uri);
                Map hashMap = new HashMap();
                hashMap.put("cardCreate", Boolean.valueOf(true));
                if (FastViewInstanceImpl.this.mAttachedRootView != null) {
                    ViewGroup viewGroup = (ViewGroup) FastViewInstanceImpl.this.mAttachedRootView.getParent();
                    if (viewGroup != null) {
                        LayoutParams layoutParams = viewGroup.getLayoutParams();
                        if (layoutParams != null) {
                            hashMap.put("width", Integer.valueOf(layoutParams.width));
                        }
                    }
                }
                FastSDKProfile.end("createInstance:" + cardRenderInfo.widgetInfo.getName());
                FastViewInstanceImpl.this.mInstance.render(cardRenderInfo.widgetInfo.getName(), cardRenderInfo.js, hashMap, cardRenderInfo.jsonParam);
            }
        };
    }

    private Bundle makePackageInfo(WidgetInfo widgetInfo) {
        Bundle bundle = new Bundle();
        FastAppInfo fastAppInfo = widgetInfo.getFastAppInfo();
        bundle.putString("packageName", widgetInfo.getId());
        bundle.putString("name", widgetInfo.getName());
        if (fastAppInfo != null) {
            setBundleValueIfNotEmpty(bundle, "packageName", fastAppInfo.getPackageName());
            setBundleValueIfNotEmpty(bundle, "name", fastAppInfo.getName());
            setBundleValueIfNotEmpty(bundle, "icon", fastAppInfo.getIcon());
            setBundleValueIfNotEmpty(bundle, CloudAccountManager.KEY_VERSION_NAME, fastAppInfo.getVersionName());
            setBundleValueIfNotEmpty(bundle, "certificate", fastAppInfo.getCertificate());
            bundle.putInt(RpkInfo.VERSIONCODE, fastAppInfo.getVersionCode());
            bundle.putInt("minPlatformVersion", fastAppInfo.getMinPlatformVersion());
        }
        return bundle;
    }

    private void runOnUIThread(Runnable runnable) {
        new Handler(this.mContext.getMainLooper()).post(runnable);
    }

    private void setBundleValueIfNotEmpty(Bundle bundle, String str, String str2) {
        if (!TextUtils.isEmpty(str2)) {
            bundle.putString(str, str2);
        }
    }

    public void attachRootView(FrameLayout frameLayout) {
        this.mAttachedRootView = frameLayout;
    }

    protected void createInstance() {
        destroyInstance();
        FastViewLogUtils.i(TAG, "createInstance");
        FastSdkLoader instance = FastSdkLoader.getInstance();
        if (instance == null) {
            FastViewLogUtils.e(TAG, "sdkLoader == null");
            return;
        }
        IFastSDKProxy fastSDKProxy = instance.getFastSDKProxy();
        if (fastSDKProxy == null) {
            FastViewLogUtils.e(TAG, "sdkProxy == null");
            return;
        }
        instance.patchResources(this.mContext);
        if (this.mWidth > 0) {
            fastSDKProxy.setViewWidth(this.mWidth);
        }
        this.mInstance = fastSDKProxy.createFastSDKInstance(this.mContext);
        if (!TextUtils.isEmpty(this.accessType)) {
            this.mInstance.setAccessType(this.accessType);
        }
        this.mInstance.registerFastRenderListener(this);
        this.mInstance.registerFastCardMessageToHost(this);
        this.mInstance.onActivityCreate();
    }

    protected void destroyInstance() {
        if (this.mInstance != null) {
            FastViewLogUtils.i(TAG, "destroyInstance");
            this.mInstance.registerFastRenderListener(null);
            this.mInstance.registerFastCardMessageToHost(null);
            this.mInstance.destroy();
            this.mInstance = null;
        }
    }

    public boolean onBack() {
        return this.mInstance != null ? this.mInstance.onActivityBack() : false;
    }

    public void onConfigurationChanged(Configuration configuration) {
        if (this.mInstance != null) {
            this.mInstance.onActivityConfigurationChanged(configuration);
        }
    }

    public void onDestroy() {
        FastViewLogUtils.i(TAG, "onDestroy");
        this.isDestroyed = true;
        if (this.mInstance != null) {
            this.mInstance.registerFastRenderListener(null);
            this.mInstance.registerFastCardMessageToHost(null);
            this.mInstance.onActivityDestroy();
            this.mInstance.clearResource();
        }
    }

    public void onException(String str, String str2) {
        FastViewLogUtils.e(TAG, "onException " + str);
        if (this.mRenderListener != null) {
            this.mRenderListener.onException(this, str, str2);
        }
    }

    public void onPause() {
        FastViewLogUtils.i(TAG, "onPause");
        if (this.mInstance != null) {
            this.mInstance.onActivityPause();
        }
    }

    public void onRefreshSuccess(int i, int i2) {
        if (this.mRenderListener != null) {
            this.mRenderListener.onRefreshSuccess(this);
        }
    }

    public void onRenderSuccess() {
        FastViewLogUtils.i(TAG, "Render| onRenderSuccess()");
        FastSDKProfile.log("onRenderSuccess");
        if (this.mRenderListener != null) {
            this.mRenderListener.onRenderSuccess(this);
        }
    }

    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        if (this.mInstance != null) {
            this.mInstance.onRequestPermissionsResult(i, strArr, iArr);
        }
    }

    public void onResume() {
        FastViewLogUtils.i(TAG, "onResume");
        if (this.mInstance != null) {
            this.mInstance.onActivityResume();
        }
    }

    public void onRoute(Object... objArr) {
        FastViewLogUtils.i("FastViewInstanceImpl", "receive on route");
        if (objArr != null) {
            if (objArr.length > 0 && (objArr[0] instanceof String) && this.mRenderListener != null) {
                this.mRenderListener.onRoute(this, (String) objArr[0]);
            }
            FastSdkLoader instance = FastSdkLoader.getInstance();
            if (instance == null) {
                FastViewLogUtils.e("FastViewInstanceImpl", "sdkLoader == null");
            } else if (instance.getHostMode() == HostMode.Lite && this.mContext != null && objArr.length > 1 && (objArr[1] instanceof Intent)) {
                new RouterEventReceiver().onReceive(this.mContext, (Intent) objArr[1]);
            }
        }
    }

    public void onStart() {
        FastViewLogUtils.i(TAG, "onStart");
        if (this.mInstance != null) {
            this.mInstance.onActivityStart();
        }
    }

    public void onStop() {
        FastViewLogUtils.i(TAG, "onStop");
        if (this.mInstance != null) {
            this.mInstance.onActivityStop();
        }
    }

    public void onViewCreated(View view) {
        FastViewLogUtils.i(TAG, "onViewCreated");
        if (this.mAttachedRootView == null) {
            FastViewLogUtils.e(TAG, "mAttachedRootView null!");
            return;
        }
        this.mAttachedRootView.getLayoutParams().height = -2;
        View childAt = ((ViewGroup) view).getChildAt(0);
        View childAt2 = ((ViewGroup) childAt).getChildAt(0);
        View childAt3 = ((ViewGroup) childAt2).getChildAt(0);
        childAt.getLayoutParams().height = -2;
        childAt2.getLayoutParams().height = -2;
        childAt3.getLayoutParams().height = -2;
        this.mAttachedRootView.removeView(view);
        this.mAttachedRootView.addView(view);
        if (this.mRenderListener != null) {
            this.mRenderListener.onViewCreated(this, view);
        }
    }

    public void ondCardMessage(String str) {
        if (this.iCardMessage != null) {
            this.iCardMessage.onCardMessage(this, str);
        } else {
            FastViewLogUtils.d("FastSDKInstance", "Render| iCardMessage is null");
        }
    }

    public void render(final String str) {
        this.mBundleLoader.execute(new Runnable() {
            public void run() {
                FastViewLogUtils.i(FastViewInstanceImpl.TAG, "render begin");
                CardRenderInfo cardRenderInfo = new CardRenderInfo();
                FastSDKProfile.begin("loadWidget");
                cardRenderInfo.widgetInfo = FastViewInstanceImpl.this.mBundleLoader.loadWidget();
                FastSDKProfile.end("loadWidget");
                FastSDKProfile.begin("getScript");
                cardRenderInfo.js = FastViewInstanceImpl.this.mBundleLoader.getScript(cardRenderInfo.widgetInfo);
                FastSDKProfile.end("getScript");
                cardRenderInfo.jsonParam = str;
                cardRenderInfo.uri = FastViewInstanceImpl.this.mBundleLoader.getUri();
                FastViewLogUtils.d(FastViewInstanceImpl.TAG, "widget name:" + cardRenderInfo.widgetInfo.getName());
                if (TextUtils.isEmpty(cardRenderInfo.js)) {
                    FastViewLogUtils.e(FastViewInstanceImpl.TAG, "card js is null, render failed.");
                    return;
                }
                FastViewLogUtils.i(FastViewInstanceImpl.TAG, "begin render on UI thread.");
                FastSDKProfile.log("beginRender");
                FastViewInstanceImpl.this.runOnUIThread(FastViewInstanceImpl.this.getUIRender(cardRenderInfo));
            }
        });
    }

    public void sendMessageToCard(String str) {
        if (this.mInstance != null) {
            this.mInstance.sendMessageToCard(str);
        }
    }
}
